package bl.user;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.List;

import client.boot.BLServiceManager;
import o.Authority;
import o.UserType;
import server.boot.ServiceLoader;
import vo.UserVo;
import vo.Vo;
import db.Database;
import blservice.user.UserBLService;

public class UserBLDriver {
	UserBLService bl;
	public static void main(String args[]) throws MalformedURLException, NotBoundException, SQLException, AlreadyBoundException{

		UserBLDriver d=new UserBLDriver();
		try {
			d.drive();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	void drive() throws RemoteException, MalformedURLException, NotBoundException, SQLException, AlreadyBoundException{
		//环境初始化
		Database.iniDatabase("localhost", 3306, "seac2", "seac2", "seac2");
		ServiceLoader.iniServiceLoader();
		BLServiceManager.setServerIP("localhost");
		bl=BLServiceManager.getUserBLService();
		
		
		
		UserVo vo=new UserVo();
		vo.setOperation("添加用户");
		vo.setName("小明");
		vo.setAuthority(Authority.最高权限);
		vo.setPassword("abc");
		vo.setNewPassword("def");
		vo.setOperator("test");
		vo.setUserType(UserType.管理员);
		System.out.println("add:");
		Vo result=bl.addUser(vo);
		if(result.isSuccessed()){
			System.out.println("添加成功");
		}else{
			System.out.println(result.getFailMessage());
		}
		System.out.println("changeAuthority:");
		result=bl.changeAuthority(vo);
		if(result.isSuccessed()){
			System.out.println("更改权限成功");
		}else{
			System.out.println(result.getFailMessage());
		}
		System.out.println("changePassword:");
		result=bl.changePassword(vo);
		if(result.isSuccessed()){
			System.out.println("更改密码成功");
		}else{
			System.out.println(result.getFailMessage());
		}
		System.out.println("checkLogin:");
		UserVo user=bl.checkLogin(vo);
		if(result.isSuccessed()){
			System.out.println("user的信息:");
			System.out.println(user.getName());
			System.out.println(user.getAuthority());
			System.out.println(user.getUserType());
		}else{
			System.out.println(result.getFailMessage());
		}
		System.out.println("deleteUser:");
		result=bl.deleteUser(vo);
		if(result.isSuccessed()){
			System.out.println("删除成功");
		}else{
			System.out.println(result.getFailMessage());
		}
		
		System.out.println("getAll:");
		List<UserVo> r=bl.getAll();
		for (UserVo o:r){
			System.out.println(o.getName());
		}
		
		System.exit(0);
	}

}
